/**
 * 
 */
package com.agilegroup.crm.ui.fragments;

import java.text.DateFormatSymbols;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockDialogFragment;
/**
 * @author hisham
 *
 */
@SuppressLint("ValidFragment")
public class DatePickerFragment extends SherlockDialogFragment implements DatePickerDialog.OnDateSetListener{

	private TextView txtDate;
	private TextView txtDay;
	@SuppressLint("ValidFragment")
	public DatePickerFragment(TextView txtDate,TextView txtDay) {
		this.txtDate=txtDate;
		this.txtDay=txtDay;
	}
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        
        DatePickerDialog dialog =  new DatePickerDialog(getActivity(), this, year, month, day);
        dialog.setTitle("Set Date");
        int sdkVersion = android.os.Build.VERSION.SDK_INT;
        if(sdkVersion>=android.os.Build.VERSION_CODES.HONEYCOMB){
        	dialog.getDatePicker().setCalendarViewShown(false);
        }
        
        return dialog;
    }
	
	public void onDateSet(DatePicker view, int year, int month, int day) {		
		txtDate.setText(new StringBuilder().append(day).append("/").append(month + 1).append("/").append(year));
		if(txtDay!=null){
			  String dayNames[] = new DateFormatSymbols().getWeekdays();
			  Calendar date1 = Calendar.getInstance();
			  date1.set(year, month, day);
			  txtDay.setText(dayNames[date1.get(Calendar.DAY_OF_WEEK)]);
		}
    }
}
